package com.young.android.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.SimpleTimeZone;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Toast;

import com.young.android.R;
import com.young.android.business.MainBusiness;
import com.young.android.dbreposit.SqliteHelper;

/**
 * 公共方法类
 * 
 * @author Young
 * 
 */
public class PubFunction {

    private static final String TAG = "PubFunction";

    public static void ShowToast(Context context, int strId) {
        // TODO Auto-generated method stub
        try {
            Toast.makeText(context, context.getResources().getString(strId), Toast.LENGTH_SHORT)
                    .show();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    public static int getCursorIntByName(Cursor cursor, String columnName) {
        return cursor.getInt(cursor.getColumnIndex(columnName));

    }

    /**
     * 关闭数据集
     * 
     * @param cur
     */
    public static void CloseCursor(Cursor cur) {
        if (cur != null)
            cur.close();
    }

    /**
     * 把资源保存成文件
     * 
     * @param srcAsset
     * @param destFile
     * @return
     */
    public static boolean SaveAssetsToFile(String srcAsset, File destFile) {
        boolean breturn = false;
        InputStream inStream = null;
        FileOutputStream fs = null;
        try {
            int byteread = 0;
            inStream = SqliteHelper.mContext.getAssets().open(srcAsset);// 载模板
            fs = new FileOutputStream(destFile.getPath());
            byte[] buffer = new byte[1024];
            while ((byteread = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();
            breturn = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                if (inStream != null)
                    inStream.close();
                if (fs != null)
                    fs.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return breturn;
    }

    /**
     * 退出程序
     * 
     * @param context
     */
    public static void ExitApplication(Context context) {
        // 关闭数据库
        MainBusiness.CloseDB();
        // 杀掉自己
        System.exit(0);
    }

    /**
     * html标签转换
     * 
     * @param html
     * @return
     */
    public static String parseHtml(String html) {
        html = Html.fromHtml(html).toString();
        return html;
    }

    /**
     * 转换日期格式
     * 
     * @param date
     * @return
     */
    public static String parseDate(String date) {
        SimpleDateFormat sdfTemp = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);

        SimpleTimeZone aZone = new SimpleTimeZone(8, "GMT");
        sdfTemp.setTimeZone(aZone);
        Date d = null;
        try {
            d = sdfTemp.parse(date);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy年MM月d日 HH:mm:ss", Locale.CHINESE);
        return sdf2.format(d);
    }

    /**
     * 对title中(')字符转移
     * 
     * @param title
     * @return
     */
    public static String parseTitle(String title) {
        return title.replace("'", "''");
    }

    /**
     * 获取字符串资源
     * 
     * @param resId
     * @return
     */
    public static String getResourcesString(int resId) {
        String sres = null;
        try {
            sres = SqliteHelper.mContext.getResources().getString(resId);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return sres;
    }

    /**
     * 获取Drawable资源
     * 
     * @param resId
     * @return
     */
    public static Drawable getResourcesDrawable(int resId) {
        Drawable d = null;
        try {
            d = SqliteHelper.mContext.getResources().getDrawable(resId);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return d;
    }

    /**
     * 消息发送
     * 
     * @param m
     * @param b
     */
    public static void sendNotify(int m, Handler handler) {
        Message msg = new Message();
        msg.what = m;
        handler.sendMessage(msg);
    }

    public static void loadAnimation(View view,int ani){
        Animation fadeInAni = AnimationUtils.loadAnimation(SqliteHelper.mContext, R.anim.fadein);
        view.startAnimation(fadeInAni);
    }
    
    /**
     * 退出程序(杀进程)
     * 
     * @param mAct
     */
    public static void killProcess(Context mAct) {

        ScreenManager.getScreenManager().popAllActivity();
        System.exit(0);

    }

}
